// Created by wangliang on 2025/4/14.
import 'package:flutter/material.dart';
import 'package:flutter_app_demo/helper/event_bus_helper.dart';
import 'package:go_router/go_router.dart';

class RouteHelper {
  RouteHelper._();

  static final RouteHelper _instance = RouteHelper._();

  static RouteHelper get instance => _instance;

  final MyRouteObserver _routeObserver = MyRouteObserver();

  MyRouteObserver get routeObserver => _routeObserver;

  String? get currentRouteName => _routeObserver.currentRouteName;
  String? get previousRouteName {
    if (_routeObserver.previousRouteName == '首页') {
      return _routeObserver.homeCurrentRouteName;
    } else {
      return _routeObserver.previousRouteName;
    }
  }

  BuildContext? get currentContext => _routeObserver.currentContext;

  replace(BuildContext context, String location) {
    context.replace(location);
    _routeObserver.replaceRouteToStack(location);
  }
}

class MyRouteObserver extends NavigatorObserver {
  String? homeCurrentRouteName;

  final List<String> _routeStack = [];
  List<String> get routerStack => List.unmodifiable(_routeStack);
  String? get currentRouteName => _routeStack.isNotEmpty ? _routeStack.last : null;
  String? get previousRouteName => _routeStack.length > 1 ? _routeStack[_routeStack.length - 2] : null;
  BuildContext? currentContext;

  @override
  void didPush(Route route, Route? previousRoute) {
    final name = _getRouteName(route);
    _updateCurrentContext(route);
    // print('didPush name $name');
    _routeStack.add(name);
    super.didPush(route, previousRoute);
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    // final name = _getRouteName(route);
    if (_routeStack.isNotEmpty) {
      _routeStack.removeLast();
      // print('WLTest didPop: $name');
      // print('Route Stack: $_routeStack');
    }
    super.didPop(route, previousRoute);
    _updateCurrentContext(previousRoute);
  }

  @override
  void didStopUserGesture() {
    super.didStopUserGesture();
    if ("web" == RouteHelper.instance.currentRouteName) {
      EventBusHelper.instance.defaultEventBus.fire(PageStopUserGestureEvent());
    }
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    final name = _getRouteName(newRoute);
    // print('WLTest didReplace name $name');
    if (_routeStack.isNotEmpty) {
      _routeStack.removeLast();
      _routeStack.add(name);
      // print('didReplace: $name');
      // print('Route Stack: $_routeStack');
    }
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    _updateCurrentContext(newRoute);
  }

  void _updateCurrentContext(Route? route) {
    currentContext = route?.navigator?.context;
  }

  void replaceRouteToStack(String location) {
    // 这里由于 replace 的页面不多，而且 _routeStack 只是埋点需要，所以这里简单处理 /login 和 /home 两个
    String name = location == '/login'
        ? '登录'
        : location == '/home'
            ? '首页'
            : location;
    if (_routeStack.isNotEmpty) {
      _routeStack.removeLast();
    }
    _routeStack.add(name);
  }

  String _getRouteName(Route? route) {
    if (route == null) return '';
    // print('根据参数处理 name ${route.settings.name} arguments: ${route.settings.arguments}');
    return route.settings.name ?? '';
  }
}
